package com.erb.controller.hzg;

import com.erb.constants.AdminProductConstants;
import com.erb.constants.LoanConstants;
import com.erb.entity.*;
import com.erb.service.cgs.SmloadService;
import com.erb.service.hzg.lonsauditService;
import com.erb.service.zwj.IProductService;
import com.erb.utils.BigDecimalUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/loan/back-audit")
public class loansmaterial {
	@Resource
	lonsauditService service;
	@Resource
	SmloadService smloadService;
	@Resource
	IProductService productService;

	@RequestMapping("/list")
	public String ht(Pager pager, Model model) {
		//分页
		pager.page(service.selProcount());
		pager.data = service.selApplyPage(pager);
		model.addAttribute("pager", pager);

		List<Map> loanList = service.auditlist();
		//时间转化存入Map
        /*for (int i = 0;i<loanList.size();i++){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String ondate = sdf.format(loanList.get(i).get("onltime"));
            loanList.get(i).put("ondate",ondate);
            String ofdate = sdf.format(loanList.get(i).get("offltime"));
            loanList.get(i).put("ofdate",ofdate);
        }*/

		model.addAttribute("loanlist", pager.data);
		return "loan/loanAudit";
	}

	@RequestMapping("/audupd")
	@ResponseBody
	public String auditupd(HttpSession session,int audid, String submitUrl) {
		LoanUser user1 = (LoanUser) session.getAttribute("loanuser");
		System.out.println("审核为：" + submitUrl);
		System.out.println("id：" + audid);
		int bts = 3;
		if (submitUrl.equals("同意")) {
			bts = LoanConstants.Loan_Audit_State_Consent;
			service.auditupd(bts, audid);
			//修改金额
			Loan loan = service.seluid(audid);
			double money = loan.getMoney();
			//查询用户金额
			LoanUser user = service.selusermoney(loan.getUid());
			double addmoney = money + user.getAvailableLimit();
			System.out.println("money "+money + "lim"+user.getAvailableLimit());
			smloadService.updusermoney(loan.getUid(),addmoney);
			//分期还款
			Loan payloan1 = smloadService.payloan(audid);
			smloadService.updateuserlimit(user1.getId(),payloan1.getMoney());
			int loandate = payloan1.getLoandate();
			double monthmoaney = payloan1.getMoney() / loandate;
			String loaninterest = payloan1.getLoaninterest();
			loaninterest = loaninterest.substring(0, loaninterest.indexOf("%"));
			double loaninterest1 = Double.parseDouble(loaninterest) / 100;
			double monthmoaney1 = monthmoaney + (monthmoaney * loaninterest1);
			Payloan payloan = new Payloan();
			payloan.setLid(payloan1.getLid());
			payloan.setPaidmoney(0.0);
			payloan.setPaymoney(monthmoaney1);
			payloan.setPayselfmoney(monthmoaney);
			payloan.setPayinterest(monthmoaney * loaninterest1);
			payloan.setPaystate(0);
			for (int i = 1; i <= loandate; i++) {
				int addpayloan = smloadService.addpayloan(i, payloan);
			}


			//添加散标
			Product product = new Product();
			product.setTotal(loan.getMoney());
			product.setHaveMoney(0.0);
			product.setLoanId(loan.getLid());
			product.setLockDateMonth(loandate);
			product.setPname(loan.getLoanname() + " - " + loan.getLoantype());
			product.setRate(BigDecimalUtil.formatDouble(loaninterest1 * 10 + 0.05));
			product.setPurchaseAmount(loan.getMoney() / 10);
			product.setStartDate(new Timestamp(System.currentTimeMillis()));
			product.setStatus(AdminProductConstants.PRODUCT_STATUS_BIDS);
			int type = AdminProductConstants.PRODUCT_REPAY_TYPE_END;
			if ("小额消费贷款".equals(loan.getLoantype()) || "公积金贷".equals(loan.getLoantype())) {
				type = AdminProductConstants.PRODUCT_REPAY_TYPE_END_MONTH;
			} else if ("万能房抵贷".equals(loan.getLoantype()) || "车主信用贷".equals(loan.getLoantype())) {
				type = AdminProductConstants.PRODUCT_REPAY_TYPE_MONTH;
			}
			product.setRepayType(type);
			product.setType(AdminProductConstants.PRODUCT_TYPE_CONVERSION_LOAN);
			productService.saveProduct(product);

		} else if (submitUrl.equals("拒绝")) {
			bts = LoanConstants.Loan_Audit_State_rRefuse;
			service.auditupd(bts, audid);
		}
		return "完成";
	}

	@RequestMapping("/selmodalapply")
	public String selmodalapply(int id, String applyMType, Model session) {
		System.out.println("id:" + id + "类型：" + applyMType);
		Loan loan = service.seluid(id);
		System.out.println("loan :" + loan.getUid());
		int appuid = loan.getUid();
		int appid = loan.getApplyid();

        /*String fields = loan.getProve();
        System.out.println("fields : "+fields);
        String[] field = fields.split(",");*/
		if (applyMType.equals("万能房抵贷")) {
			List<Houseimf> houseimfs = service.Houselist(appid, appuid);
			if (houseimfs.size() != 0) {
				session.addAttribute("houseimfs", houseimfs);
				return "loan/loanShowhouseApply";
			} else {
				return "loan/loanShowallApply";
			}

		} else if (applyMType.equals("汽车抵押贷")) {
			List<Carimf> carmap = service.idshowlist(appid, appuid);
			session.addAttribute("carmap", carmap);
			return "loan/loanShowApply";
		}
		return "loan/loanShowallApply";
	}


}
